package de.gwtprime.server.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import de.gwtprime.server.model.Article;
import de.gwtprime.server.sql.HibernateEntityMangerSingelton;
import de.gwtprime.server.sql.SQLConnect;

public class PingServlet extends HttpServlet {

	/**
	 * Logger
	 */
	private static Logger logger = Logger.getLogger(PingServlet.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Servlet pings the database to keep it alive
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			
			// sql connect
			SQLConnect.getInstance().ping();
			
			// hibernate manager
			HibernateEntityMangerSingelton.getEntityManagerFactory();
			
			Article.loadLatestArticles(5);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		
		logger.error("Database was successfully pinged.");
	}

}
